package com.suping.springbuckssp.service;

import com.suping.springbuckssp.dao.OrderMapper;
import com.suping.springbuckssp.model.Order;
import com.suping.springbuckssp.model.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * @author SuPing
 * @date 2022/12/29 13:31
 **/
@Slf4j
@Service
@Transactional
public class OrderService {
  @Autowired
  private OrderMapper orderMapper;

  public Order createOrder(String customer) {
    Order order = new Order();
    order.setCustomer(customer);
    order.setState(OrderState.INIT);
    orderMapper.insert(order);
    log.info("New Order: {}", order);
    return order;
  }

  public boolean updateState(Order order, OrderState state) {
    if (state.compareTo(order.getState()) <= 0) {
      log.warn("Wrong State order: {}, {}", state, order.getState());
      return false;
    }
    order.setState(state);
    orderMapper.updateByPrimaryKey(order);
    log.info("Updated Order: {}", order);
    return true;
  }

}
